classdef TestDotsTheScreen < dotsTestCase
    
    properties
        theScreen;
    end
    
    methods
        function self = TestDotsTheScreen(name)
            self = self@dotsTestCase(name);
        end
        
        function setUp(self)
            self.setUp@dotsTestCase;
            
            dotsTheScreen.reset;
            self.theScreen = dotsTheScreen.theObject;
        end
        
        function tearDown(self)
            self.tearDown@dotsTestCase;
            dotsTheScreen.reset;
        end
        
        function testPriority(self)
            absTolerance = 1;
            
            self.theScreen.open;
            requested = self.theScreen.priority;
            actual = Priority;
            assertElementsAlmostEqual(requested, actual, 'absolute', absTolerance, ...
                'should have set a process priority when window opened');
            
            self.theScreen.close;
            actual = Priority;
            assertEqual(0, actual, ...
                'should have set normal process priority when window closed');
        end
        
        function testFlips(self)
            
            self.theScreen.open;
            
            % report increasing flip times
            when(1) = self.theScreen.nextFrame;
            when(2) = self.theScreen.nextFrame;
            when(3) = self.theScreen.nextFrame;
            assertTrue(all(diff(when) > 0), 'flip times should be increasing')
            
            self.theScreen.close;
        end
        
        function testRedundantOpenClose(self)
            self.theScreen.open;
            self.theScreen.open;
            self.theScreen.close;
            self.theScreen.open;
            self.theScreen.close;
            self.theScreen.close;
        end
        
        function testResetShouldCloseWindow(self)
            self.theScreen.open;
            dotsTheScreen.reset;
            
            assertTrue(self.theScreen.windowNumber < 0, 'the screen should know its window has closed')
            
            openWindows = Screen('Windows');
            assertTrue(isempty(openWindows), 'reset should close all windows')
            
            windowNumber = dotsTheSwitchboard.getSharedPropertyValue('windowNumber');
            assertTrue(windowNumber < 0, 'reset should invalidate windowNumber shared value')
        end
        
        function testIndependentOfSwitchboard(self)
            dotsTheScreen.reset;
            dotsTheSwitchboard.reset;
            
            self.theScreen.open;
            assertTrue(self.theScreen.windowNumber > 0, ...
                'should have valid windowNumber without help from theSwitchboard')

            self.theScreen.close;
            assertTrue(self.theScreen.windowNumber < 0, ...
                'should have invalid windowNumber without help from theSwitchboard')
        end
    end
end